import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/utils/base_component_utils.dart';
import 'package:x3_form_core/business_components/base_request_form_label.dart';

class BaseRequestFormContainer extends StatelessWidget {
  BaseRequestFormContainer({Key key, @required this.child, @required this.request, this.onPress, this.suffixIcon, this.showSuffixIcon}) : super(key: key);

  /// 入参
  final BaseHttpRequestModel request;

  /// 主体
  final Widget child;

  /// 点击事件
  final VoidCallback onPress;

  /// 是否显示小图标
  final bool showSuffixIcon;

  /// 小图标
  final Widget suffixIcon;

  @override
  Widget build(BuildContext context) {
    bool isClickEnabled = request.accessMode == BaseHttpAccessModelEnum.WebReadWrite;
    String name = this.request.name;
    return InkWell(
      onTap: _onTap(context, isClickEnabled),
      child: Container(
        constraints: BoxConstraints(minHeight: 48),
        alignment: Alignment.centerLeft,
        child: Row(
          children: [
            name == 'noShowLabel' ? SizedBox( width: 0 ) : _buildLabelWidget(),
            name == 'noShowLabel' ? SizedBox( width: 0 ) : SizedBox(width: 12),
            Expanded(child: Container(alignment: Alignment.centerLeft, child: this.child)),
            SizedBox(width: 4),
            Container(margin: EdgeInsets.only(right: this.showSuffixIcon == true ? 8 : 0), child: _buildRightIcon(context, isClickEnabled)),
          ],
        ),
      ),
    );
  }

  SizedBox _buildLabelWidget() {
    return SizedBox(
      width: this.request.labelWidth,
      child: FittedBox(
        fit: BoxFit.scaleDown,
        alignment: Alignment.centerLeft,
        child: BaseRequestFormLabel(request: this.request),
      ),
    );
  }

  Widget _buildRightIcon(BuildContext context, bool isClickEnabled) {
    Widget child = this.suffixIcon;
    if (this.suffixIcon == null) child = BaseComponentUtils.buildListViewRightIcon(context);
    return this.showSuffixIcon == true ? child : SizedBox();
  }

  Function _onTap(BuildContext context, bool isClickEnabled) {
    if (isClickEnabled && this.onPress != null) {
      return () {
        FocusScope.of(context).requestFocus(FocusNode());
        this.onPress();
      };
    }
    return null;
  }
  
}
